import DataType from './core/data-type'
import Storage from './core/storage'
import Query from './core/query'
import Rest from './core/rest'
import Inner from './core/inner'
import Router from './core/router'
import FormSeach from './core/form-search'
import FormSubmit from './core/form-submit'
import ProjectFn from '$project/fn'

const msg 		= Rest.lazy(()=>import('@antd/msg'))
const confirm 	= Rest.lazy(()=>import('@antd/confirm'))
/* ====================================== 全局变量及方法  ====================================== */
const Index = {
	...DataType,
	...Storage,
	...Query,
	...Rest,
	...Inner,
	...Router,
	...FormSeach,
	...FormSubmit,
	...ProjectFn,
	msg: {
		success: v => msg.then( f => f.success(v)),
		warning: v => msg.then( f => f.warning(v)),
		error: v => msg.then( f => f.error(v))
	},
	confirm: v => confirm.then( f => f(v)),
	// 获取权限数据
	setPerm( data, perm ){
		const route = $.route()
		let { permissions } = route.meta
		if($fn.isString( permissions )){
			permissions = permissions.replace(/user:/g,'').split(',')
		}
		// perm = $fn.getResult( perm )
		perm = perm ? perm : permissions
		if( perm ){
			if($fn.hasArray( perm )){
				const d = data.filter( v => perm.includes( v.perm ) || !v.perm )
				return [ ...d ]
			}else{
				return data
			}
		}else{
			return data
		}
	},
	// 表单验证
	valid: async (value, { bool, msg }) => bool ?  Promise.resolve() : Promise.reject(msg),
	validReg: (value, { reg, msg }) => $fn.valid( value, { bool: reg.test(value), msg } )
}

export default Index